package main.java.com.lee.juc.blockingqueue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;

/**
 * @Description TODO
 * @Author winston
 * @DateTime 2022/2/17
 */
public class ArrayBlockingQueueDemo {

    static ArrayBlockingQueue queue = new ArrayBlockingQueue<Integer>(10, true);
    public static void main(String[] args) {

        ExecutorService producerPool = Executors.newFixedThreadPool(10);

        new Thread(() -> {
            consumer();
        }).start();

        IntStream.rangeClosed(1, 100).forEach(i->{
            producerPool.submit(()->{
                try {
                    producer(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        });

    }

    public static void producer(Integer value) throws InterruptedException {
        queue.put(value);
        queue.remove();
        queue.element();
        System.out.println(value+" : 生产成功.");
    }

    public static void consumer() {
        IntStream.rangeClosed(1, 100).forEach(i->{
            try {
                System.out.println(queue.take());
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }
}
